package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.feignclient.DictFeignClient;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Resource
    private DictFeignClient dictFeignClient;

    /**
     * 获取就诊人列表
     *
     * @param userInfoId
     * @return
     */
    @Override
    public List<Patient> findAllPatient(Long userInfoId) {

        // 封装查询条件
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userInfoId);
        // 调用mapper查询列表
        List<Patient> patientList = patientMapper.selectList(queryWrapper);
        patientList.stream().forEach(this::pakgePatient);

        return patientList;
    }

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = patientMapper.selectById(id);
        patient = this.pakgePatient(patient);
        return patient;
    }

    /**
     *  这个方法封装就诊人的列表的其他信息
     * @param patient
     * @return
     */
    private Patient pakgePatient(Patient patient) {

        // 联系人证件
        String name = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //根据证件类型编码，获取证件类型具体值
        String certificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型
        String contactsCertificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        // 封装为map集合的属性中
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());

        // 返回就诊人数据
        return patient;

    }
}
